/**
 * 用户操作日志
 */
var $userOptListTable;
var UserOptList = {

    init: function () {

        // init app select
        fmt_ymd_hms('start_time_input');
        fmt_ymd_hms('end_time_input');
        init_user_opt_type_sbox("search_opt_type", true);
        init_app_id_sbox('search_appId', null, true);
        //
        UserOptList.createTable();

        //查询
        $("#search_btn").click(function () {
            var params = {};
            params.startTime = new Date($("#start_time_input").val()).getTime() || null;
            params.endTime = new Date($("#end_time_input").val()).getTime() || null;
            params.areaId = get_area_id_from_session();
            params.appId = $("#search_appId").val() || null;
            params.optType = $("#search_opt_type").val() || null;
            //
            UserOptList.reloadTable(params);
        });

        //重置
        $("#reset_search_btn").click(function () {
            $("#start_time_input").val("");
            $("#end_time_input").val("");
            $("#search_appId").val("");
            $("#search_opt_type").val("");
            //
            $("#search_btn").click();
        });
    },

    //重新加载数据
    reloadTable: function (params) {
        if ($userOptListTable) {
            $userOptListTable.bootstrapTable('refresh', {query: params, url: ApiConfig.USER_OPS_LIST});
        }
    },

    createTable: function () {
        var params = {};
        $userOptListTable = $("#user_opt_list_table").bootstrapTable({
            url: ApiConfig.USER_OPS_LIST,
            queryParams: function (queryParam) {
                params.offset = queryParam.pageSize * (queryParam.pageNumber - 1);
                params.limit = queryParam.pageSize;
                params.areaId = get_area_id_from_session();
                return params;
            },
            queryParamsType: "no",
            method: 'post',
            contentType: 'application/json',
            dataType: 'json',
            pageNumber: 1,
            pageSize: 10,
            pageList: [10, 20, 50, 100],
            sidePagination: 'server',
            pagination: true,
            smartDisplay: true,
            responseHandler: function (resp) {
                if (resp.status === 0) {
                    var dto = {};
                    dto.total = 0 || resp.content.total;
                    dto.rows = resp.content.rows || [];
                    return dto;
                } else {
                    return {
                        total: 0,
                        rows: []
                    }
                }
            },
            rowStyle: function (row, index) {
                var classes = ['info'];
                if (index % 2 === 0) {
                    return {
                        classes: classes[0]
                    };
                } else {
                    return {};
                }
            },
            columns: [{
                field: 'index',
                title: '序号',
                formatter: function (value, row, index) {
                    index = index + 1;
                    return index;
                }
            }, {
                field: 'createTime',
                title: '操作时间',
                formatter: function (value, row, index) {
                    return date_format(value);
                }
            }, {
                field: 'loginUser',
                title: '操作人'
            }, {
                field: 'appId',
                title: '应用编号'
            }, {
                field: 'areaId',
                title: '区域编号'
            }, {
                field: 'optType',
                title: '操作类型',
                formatter: function (value, row, index) {
                    return get_user_opt_type_lab(value);
                }
            }, {
                field: 'pathName',
                title: '路径名称',
                align: 'left',
            }, {
                field: 'content',
                title: '操作内容',
                align: "left",
                formatter: function (value, row, index) {
                    return mask_text(value);
                }
            }, {
                field: 'operation',
                title: '操作',
                align: "center",
                events: {
                    'click #view_detail_btn': function (e, value, row, index) {
                        $("#user_opt_detail_form input[type='text']").each(function () {
                            let id = $(this).attr("id");
                            let value = row["" + id + ""];
                            if (id == "optType") {
                                value = get_user_opt_type_lab(value);
                            } else if (id == "createTime") {
                                value = date_format(value);
                            }
                            $("#" + id).val(value);
                        });
                        $("#logContent").val(row.content);
                        //
                        $("#user_opt_detail_modal").modal();
                    }
                },
                formatter: function (value, row, index) {
                    var html = "<button class='btn btn-primary ' id='view_detail_btn'>查看</button>";
                    return html;
                }
            }]
        });
    }

};
UserOptList.init();
